package com.gopay.common.cps.manager.order.impl;

import com.gopay.common.cps.dao.order.CpsGeneralMainOrderQueryDAO;
import com.gopay.common.cps.manager.order.CpsGeneralMainOrderQueryManager;
import com.gopay.common.domain.cps.CpsGenMainOrder;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 主订单查询manager
 * 
 * @CpsGeneralMainOrderQueryManagerImpl.java
 * @author MA HUAN
 * @2013-1-22 上午10:35:53 www.gopay.com.cn Inc.All rights reserved.
 */
@Service("cpsGeneralMainOrderQueryManager")
public class CpsGeneralMainOrderQueryManagerImpl implements CpsGeneralMainOrderQueryManager {

    @Resource(name = "cpsGeneralMainOrderQueryDAO")
    private CpsGeneralMainOrderQueryDAO cpsGeneralMainOrderQueryDAO;

    @Override
    public CpsGenMainOrder get(Serializable id) {
        // TODO Auto-generated method stub
        return cpsGeneralMainOrderQueryDAO.find(CpsGenMainOrder.class, id);
    }

    @Override
    public List<CpsGenMainOrder> get(Map<Object, Object> paraMap) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<CpsGenMainOrder> getAll() {
        // TODO Auto-generated method stub
        return cpsGeneralMainOrderQueryDAO.findAll(CpsGenMainOrder.class);
    }

    @Override
    public List<CpsGenMainOrder> findCpsGenMainOrderByMerIdAndMerOrder(String merId, String merOrder) {
        // TODO Auto-generated method stub
        return cpsGeneralMainOrderQueryDAO.findCpsGenMainOrderByMerIdAndMerOrder(merId, merOrder);
    }
	
	private final String FIND_MAIN_ORDER_BY_MERTXNTM_AND_MERORDERID_HQL = "FROM CpsGenMainOrder r WHERE r.merTxnTm >=to_timestamp(:merTxnTmMin, 'yyyyMMddhh24missff') and r.merTxnTm <=to_timestamp(:merTxnTmMax, 'yyyyMMddhh24missff')  and r.merOrderId=:merOrderId and r.merId=:merId";
	private final String FIND_SUCC_MAIN_ORDER_BY_MERTXNTM_AND_MERORDERID_HQL = FIND_MAIN_ORDER_BY_MERTXNTM_AND_MERORDERID_HQL
			+ " and (r.txnStaCd=20000 or r.txnStaCd=30102) ";

    @Override
    public CpsGenMainOrder findCpsGenMainOrderByMerTxnTmAndMerOrderId(String merTxnTm, String merOrderId, String merId) {
        Map<String, Object> para = new HashMap<String, Object>();
        para.put("merTxnTmMin", merTxnTm+"000000");
        para.put("merTxnTmMax", merTxnTm+"999999");
        para.put("merOrderId", merOrderId);
        para.put("merId", merId);
        List<CpsGenMainOrder> mainList = cpsGeneralMainOrderQueryDAO.findByHql(
                this.FIND_MAIN_ORDER_BY_MERTXNTM_AND_MERORDERID_HQL, para);

        if (mainList == null || mainList.size() == 0) {
            return null;
        }
        return mainList.get(0);
    }

    @Override
    public CpsGenMainOrder findSuccCpsGenMainOrderByMerTxnTmAndMerOrderId(String merTxnTm, String merOrderId,
            String merId) {
        Map<String, Object> para = new HashMap<String, Object>();
        para.put("merTxnTmMin", merTxnTm+"000000");
        para.put("merTxnTmMax", merTxnTm+"999999");
        para.put("merOrderId", merOrderId);
        para.put("merId", merId);
        List<CpsGenMainOrder> mainList = cpsGeneralMainOrderQueryDAO.findByHql(
                this.FIND_SUCC_MAIN_ORDER_BY_MERTXNTM_AND_MERORDERID_HQL, para);

        if (mainList == null || mainList.size() == 0) {
            return null;
        }
        return mainList.get(0);
    }

    @Override
    public CpsGenMainOrder findByRelevanceGopayOrderId(String relevanceGopayOrderId) {
        return cpsGeneralMainOrderQueryDAO.findByRelevanceGopayOrderId(relevanceGopayOrderId);
    }

    @Override
    public List<CpsGenMainOrder> findAllByRelevanceGopayOrderId(String relevanceGopayOrderId){
        return cpsGeneralMainOrderQueryDAO.findByRelevanceGopayOrderIds(relevanceGopayOrderId);
    }

    @Override
    public CpsGenMainOrder findByRelevanceGopayOrderIdAndGopayIntTxnCd(String relevanceGopayOrderId,String gopayIntTxnCd) {
        return cpsGeneralMainOrderQueryDAO.findByRelevanceGopayOrderIdAndGopayIntTxnCd(relevanceGopayOrderId,gopayIntTxnCd);
    }

    @Override
    public List<CpsGenMainOrder> findListByRelevanceGopayOrderIdAndGopayIntTxnCd(String relevanceGopayOrderId, String gopayIntTxnCd) {
        return cpsGeneralMainOrderQueryDAO.findListByRelevanceGopayOrderIdAndGopayIntTxnCd(relevanceGopayOrderId,gopayIntTxnCd);
    }

    @Override
    public List<CpsGenMainOrder> findListByRelevanceGopayOrderIdAndGopayIntTxnCdRecvAcct(String relevanceGopayOrderId,String gopayIntTxnCd,String recvAcct){
        return cpsGeneralMainOrderQueryDAO.findListByRelevanceGopayOrderIdAndGopayIntTxnCdRecvAcct(relevanceGopayOrderId,gopayIntTxnCd,recvAcct);
    }

    @Override
    public List<CpsGenMainOrder> getMainOrders(int txnStaCd, int pageIndex, int pageSize) {
        return cpsGeneralMainOrderQueryDAO.getMainOrders(txnStaCd, pageIndex, pageSize);
    }

    @Override
    public int getMainOrderCount(int txnStaCd) {
        return cpsGeneralMainOrderQueryDAO.getMainOrderCount(txnStaCd);
    }

	@Override
	public List<CpsGenMainOrder> getChildRelevanceGopayOrder(String orderId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(CpsGenMainOrder.class);
		criteria.add(Restrictions.eq("relevanceGopayOrderId", orderId));
		List<CpsGenMainOrder> list = cpsGeneralMainOrderQueryDAO.findByCriteria(criteria);
		return list;
	}

	private final String FIND_01724_BY_MERID_AND_MERORDERID = "FROM CpsGenMainOrder r WHERE r.gopayIntTxnCd=:gopayIntTxnCd and r.merOrderId=:merOrderId and r.merId=:merId";
	@Override
	public CpsGenMainOrder findByMerOrderIdAndAndMerIdAndGopayIntTxnCd(String merOrderId, String merId,
			String gopayIntTxnCd) {
		Map<String, Object> para = new HashMap<String, Object>();
		para.put("merOrderId", merOrderId);
		para.put("merId", merId);
		para.put("gopayIntTxnCd", gopayIntTxnCd);
        List<CpsGenMainOrder> mainList = cpsGeneralMainOrderQueryDAO.findByHql(this.FIND_01724_BY_MERID_AND_MERORDERID, para);
        if (mainList == null || mainList.size() == 0) {
            return null;
        }
        return mainList.get(0);
	}

	@Override
	public CpsGenMainOrder findCpsGenMainOrderByOrderId(String merOrderId) {
		// TODO Auto-generated method stub
		return cpsGeneralMainOrderQueryDAO.find(CpsGenMainOrder.class, merOrderId);
	}

    @Override
    public boolean findExistByIntTxnCdCustId(String tranType,String custId) {
        return cpsGeneralMainOrderQueryDAO.findExistByIntTxnCdCustId(tranType,custId);
    }

    @Override
    public boolean findRefundExistByRelevanceId(String orderId) {
        return cpsGeneralMainOrderQueryDAO.findRefundExistByRelevanceId(orderId);
    }

    @Override
    public List<CpsGenMainOrder> findOrdersByRelevanceGopayOrderIdAndGopayIntTxnCd(String relevanceGopayOrderId, String gopayIntTxnCd) {
        return cpsGeneralMainOrderQueryDAO.findOrdersByRelevanceGopayOrderIdAndGopayIntTxnCd(relevanceGopayOrderId,gopayIntTxnCd);
    }

}
